package com.edu.ujn.myh.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.edu.ujn.myh.dao.IApplyDao;
import com.edu.ujn.myh.domain.ArticleInfo;
import com.edu.ujn.myh.domain.CategoryInfo;
import com.edu.ujn.myh.domain.CollegeInfo;
import com.edu.ujn.myh.domain.OpinionsInfo;
import com.edu.ujn.myh.domain.PrivInfo;
import com.edu.ujn.myh.domain.ProfessorInfo;
import com.edu.ujn.myh.domain.UserInfo;
import com.edu.ujn.myh.service.IApplyService;
import com.edu.ujn.myh.utils.Pager;
import com.edu.ujn.myh.utils.StringToArray;
/**
 * 
 * @作者 马永华
 * @日期 2010-4-14下午10:43:10
 * @版本 1.0.0
 */
public class ApplyServiceImpl implements IApplyService {

	private IApplyDao applyDao = null;

	// 获得种类列表
	public List getCategory() {
		List list = null;
		String sql = "from CategoryInfo cateGoryInfo";
		try {
			list = applyDao.getCategory(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 获得院系列表
	public List getCollege() {
		List list = null;
		String sql = "from CollegeInfo collegeInfo";
		try {
			list = applyDao.getCollege(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 将申请信息存入数据库
	public String applyInfoSave(ArticleInfo articleInfo) {
		String str = "sucess";
		try {
			applyDao.saveApplyInfo(articleInfo);
		} catch (Exception e) {
			str = "error";
			e.printStackTrace();
		}
		return str;
	}

	// 获得未分配的稿件列表
	public List getArticleList(int type) {
		List list = null;
		String sql = "from ArticleInfo articleInfo where articleInfo.fpState ="
				+ type + "order by articleInfo.categoryInfo";
		try {
			list = applyDao.getArticleList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 获得专家列表
	public List getProfessor(String type) {
		String sql = "";
		List list = null;
		if (type == "") {
			sql = "from ProfessorInfo professorInfo";
		} else {
			sql = "from ProfessorInfo professorInfo where professorInfo.area = '"
					+ type + "'";
		}
		try {
			list = applyDao.getProfessor(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	// 将稿件专家信息存入数据库
	public Map saveAP(String[] article, String[] profess, String processId) {
		Map articleMap = new HashMap();
		Map professMap = new HashMap();
		Map returnMap = new HashMap();
		// 将稿件信息放入map
		for (int i = 0; i < article.length; i++) {
			String[] temp = StringToArray.stringToArray(article[i], ";");
			articleMap = StringToArray.arrayToMap(temp, articleMap);
		}
		// 将专家信息放入map
		for (int j = 0; j < profess.length; j++) {
			String[] temp = StringToArray.stringToArray(profess[j], ";");
			professMap = StringToArray.arrayToMap(temp, professMap);
		}
		try {
			applyDao.saveAP(articleMap, professMap, processId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 组织返回数据
		Set pKeySet = professMap.keySet();
		for (Iterator iter = pKeySet.iterator(); iter.hasNext();) {
			String cateName = (String) iter.next();
			if (articleMap.containsKey(cateName)) {
				List list = (List) professMap.get(cateName);
				for (int i = 0; i < list.size(); i++) {
					String idStr = (String) list.get(i);
					String id[] = StringToArray.stringToArray(idStr, ",");
					for (int j = 0; j < id.length; j++) {
						returnMap.put(id[j], articleMap.get(cateName));
					}
				}
			}
		}
		return returnMap;
	}

	// 获得专家信息
	public ProfessorInfo getProfessor(ProfessorInfo professorInfo) {
		String sql = "from ProfessorInfo p where p.userName = '"
				+ professorInfo.getUserName() + "'";
		List list = null;
		ProfessorInfo p = null;
		try {
			list = applyDao.getProfessor(sql);
		} catch (Exception e) {

		}
		if (list != null && list.size() > 0) {
			p = (ProfessorInfo) list.get(0);
		}
		return p;
	}

	// 获得article信息
	public ArticleInfo getArticleInfo(ArticleInfo articleInfo) {
		String sql = "from ArticleInfo articleInfo where articleInfo.AId ="+ articleInfo.getAId();
		List list = applyDao.getArticleList(sql);
		ArticleInfo ai = null;
		if(list!=null&&list.size()!=0){
			ai = (ArticleInfo)list.get(0);
		    String hql = "from OpinionInfo op where op.aId ="+ai.getAId();
		    List opList = applyDao.getOpinionsInfo(hql);
		    ai.setOpinonInfo(opList);
	}
//		if (list != null && list.size() != 0) {
//			ai = (ArticleInfo) list.get(0);
//			// 处理article的作者意见信息
//			String opinions = ai.getOpinions();
//			List o = new ArrayList();
//			if (opinions != null&&!"".equals(opinions)) {
//				String[] str1 = opinions.split("<UJN>");
//				if (str1.length > 0&&!"".equals(str1)) {
//					for (int i = 0; i < str1.length; i++) {
//						OpinionsInfo opinion = new OpinionsInfo();
//						o.add(opinion);
//						List opinin = new ArrayList();
//						List score = new ArrayList();
//						opinion.setOpinions(opinin);
//						opinion.setScore(score);
//						String opininos1 = str1[i];
//						if(!"".equals(opininos1)){
//							String str2[] = opininos1.split("<O>");
//							if (str2.length > 0) {
//								for (int j = 0; j < str2.length; j++) {
//									String opininos2 = str2[j];
//									String str3[] = opininos2.split("<S>");
//									score.add(str3[0]);
//									opinin.add(str3[1]);
//								}
//							}
//						}
//					
//					}
//				}
//
//			}
//			ai.setOpinonInfo(o);
//		}
		
		
		
		return ai;
	}

	// 获得稿件列表
	public List getArticleInfo(ArticleInfo articleInfo, int type) {
		String sql = "";
		if (type == 1) {
			sql = "from ArticleInfo articleInfo where articleInfo.processId ='"
					+ articleInfo.getProcessId()
					+ "' order by articleInfo.score";
		}
		List list = applyDao.getArticleList(sql);
		return list;
	}

	// 更新article
	public String upDateArticle(List list) {
		try {
			for (int i = 0; i < list.size(); i++) {
				ArticleInfo articleInfo = (ArticleInfo) list.get(i);
				String sql = "update ArticleInfo articleInfo set articleInfo.spState ="
						+ articleInfo.getSpState()
						+ "where articleInfo.AId ="
						+ articleInfo.getAId();
				applyDao.updateArticle(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		if (list != null) {

		}
		return "success";
	}

	// 更新article
	public String upDateArticle(ArticleInfo articleInfo) {
		try {
//			String sql = "update t_article Set a_score=(( select a_score from t_article where A_ID="
//					+ articleInfo.getAId()
//					+ " )+"
//					+ articleInfo.getScore()
//					+ ")/2,a_opinions = (( select a_opinions from t_article where A_ID="
//					+ articleInfo.getAId()
//					+ " )+'<UJN>"
//					+ articleInfo.getOpinions()
//					+ "') where a_id ="
//					+ articleInfo.getAId();
//			applyDao.updateArticleUseSQL(sql);

			applyDao.updateArticle(articleInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	// 得到已经审批过的列表
	public List getProfessArticle(ProfessorInfo pi, Pager page) {
		List list = null;
		try {
			String sql = "from ArticleProInfo a where a.professorInfo.id="
					+ pi.getId();
			list = applyDao.getProfessArticle(sql, page);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	// 分页查询文章列表
	public List getArticleList(Pager page, int type, UserInfo userInfo) {
		List list = null;
		String sql = "";
		String condition = type == 0 ? "articleInfo.spState=0"
				: type == 1 ? "articleInfo.spState=1"
						: type == 2 ? "articleInfo.spState=2"
								: type == 3 ? "articleInfo.spState=3"
										: type == 4 ? "articleInfo.spState=4"
												: type == 5 ? "articleInfo.spState=5"
														: "articleInfo.spState<100";
		if (userInfo == null) {
			sql = "from ArticleInfo articleInfo where " + condition
					+ " order by articleInfo.processId";
		} else {
			sql = "from ArticleInfo articleInfo where " + condition
					+ " and articleInfo.userInfo.userName='"
					+ userInfo.getUserName()
					+ "' order by articleInfo.createDate";
		}
		System.out.println(sql);
		list = applyDao.getArticleList(sql, page);
		return list;
	}

	// 根据ID 获得文章列表
	public List articleList(int[] articleId) {
		List list = null;
		try {
			list = applyDao.updateArticle(articleId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}
//获得权限列表
	public List getPrivList() {
		List list = applyDao.getPrivList();
		return list;
	}
//	更新权限信息
	public String modifyPriv(PrivInfo privInfo) {
		String sql = "update PrivInfo p set p.privName = '"+privInfo.getPrivName()+"' where p.id ="+privInfo.getId();
		try{
			applyDao.updatePriv(sql);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
//更新种类信息
	public String modifyCategory(CategoryInfo categoryInfo) {
		String sql = "update CategoryInfo c set c.name = '"+categoryInfo.getName()+"' where c.id ="+categoryInfo.getId();
		try{
			applyDao.updatePriv(sql);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
//	更新院系信息
	public String modifyCollege(CollegeInfo collegeInfo) {
		String sql = "update CollegeInfo c set c.name = '"+collegeInfo.getName()+"' where c.id ="+collegeInfo.getId();
		try{
			applyDao.updatePriv(sql);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
//	添加种类
	public String categoryAdd(CategoryInfo categoryInfo) {
		try{
			applyDao.saveObject(categoryInfo);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
//  添加院系
	public String collegeAdd(CollegeInfo collegeInfo) {
		try{
			applyDao.saveObject(collegeInfo);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
// 保存专家信息
	public String saveProf(List list) {
		try{
			applyDao.saveproInfo(list);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
	
	public IApplyDao getApplyDao() {
		return applyDao;
	}

	public void setApplyDao(IApplyDao applyDao) {
		this.applyDao = applyDao;
	}


}
